<?php
/* 
 * Build charts
 * TODO Expected T3/linked
 * Big charts
 *
 */

foreach($monthly as $key => $value) {
    $mmax = 35;
    $monmax = 1;
    $monmin = 13;
    $imgurl2='';
    $imgurl3='';
    $imgurl4='';
    $imgurl5=''; 
    $imgurl='<img src="http://chart.apis.google.com/chart?';

    // Axis Labels
    $imgurl = $imgurl.'chxl=1:';
    foreach ($monthly[$key] as $key1 => $value)
        foreach ($monthly[$key][$key1] as $key2 => $value){
            $imgurl = $imgurl.'|'.$key2;
            if (isset($monthly[$key][$key1][$key2]['T3'])) $imgurl3 = $imgurl3.','.$monthly[$key][$key1][$key2]['T3'];
                else $imgurl3 = $imgurl3.',0';
            $imgurl4 = $imgurl4.','.($monthly[$key][$key1][$key2]['Closed'] - $monthly[$key][$key1][$key2]['T3']);
            if (isset($monthly[$key][$key1][$key2]['linked'])) $imgurl5 = $imgurl5.','.$monthly[$key][$key1][$key2]['linked'];
                else $imgurl5 = $imgurl5.',0';
            $imgurl2 = $imgurl2.','.((int)$monthly[$key][$key1][$key2]['Closed'] / 2);
            //echo $imgurl2.'--';
            if ($mmax < $monthly[$key][$key1][$key2]['Closed']) $mmax = $monthly[$key][$key1][$key2]['Closed'];
            if ($monmin > $key2) $monmin = $key2;
            if ($monmax < $key2) $monmax = $key2;
       }
   
    //Axis Ranges
    $imgurl = $imgurl.'&chxr=0,0,'.($mmax+5);       //Max high (SRs closed)^
    $imgurl = $imgurl.'|1,'.$monmin.',';
    $imgurl = $imgurl.$monmax;                      //Max month -->
    //Visible Axes
    $imgurl = $imgurl.'&chxt=y,x';
    //Bar  width and spacing
    $imgurl = $imgurl.'&chbh=12,8,0';
    //Chart Size
    $imgurl = $imgurl.'&chs=250x150';
    //Chart Type
    $imgurl = $imgurl.'&cht=bvs';
    //Series Colors
    $imgurl = $imgurl.'&chco=FF0000,008000,0000FF,00FF00';
    //Scales for text format with custom range
    $imgurl = $imgurl.'&chds=0,'.($mmax+5).',0,'.($mmax+5).',0,'.($mmax+5).',0,'.($mmax+5);
    //Chart Data String
    $imgurl = $imgurl.'&chd=t2:';
    if (strpos($imgurl2,',')==0) $imgurl2 = substr($imgurl2, 1);
    if (strpos($imgurl3,',')==0) $imgurl3 = substr($imgurl3, 1);
    if (strpos($imgurl4,',')==0) $imgurl4 = substr($imgurl4, 1);
    if (strpos($imgurl5,',')==0) $imgurl5 = substr($imgurl5, 1);
    $imgurl = $imgurl.$imgurl3.'|'.$imgurl4.'|'.$imgurl5.'|'.$imgurl2;
    //Chart legend text and style
    //$imgurl = $imgurl.'&chdl=T3|T2|KCS+linked|KCS+linked+adequate+minimum';
    //Chart legend text and style
    //$imgurl = $imgurl.'&chdlp=t';
    //
    $imgurl = $imgurl.'&chg=0,-1,1,1';
    $imgurl = $imgurl.'&chm=N,FF0000,0,-1,8|N,008000,1,-1,8|D,0000FF,2,0,3|D,00FF00,3,0,1';
    //
    $imgurl = $imgurl.'&chtt=';
    $imgurl = $imgurl.str_replace(", "," ",$key);
    $imgurl = $imgurl.'" width="250" height="150" alt="';
    $imgurl = $imgurl.str_replace(", "," ",$key).'"/>';

    echo $imgurl;
}
echo '<br><img src="img/legend.png">';
// Yearly - Circles :)
//echo '<pre>'; print_r($monthly); echo '</pre>';

?>
